$(function() {
	$('.bottom').on('tap', function() {
		$('.zhifu').stop().fadeIn();
		$('.nianxian').animate({
			"bottom": "0"
		}, 500);
	});

	$('.nianxian-btn-b').on('tap', function() {
		$('.zhifu').stop().fadeOut();
		$('.nianxian').animate({
			"bottom": "-7.4rem"
		}, 500);
	});

	//年费
	$('.add-r').on('tap', function() {
		var addNum = $(this).prev().html();
		addNum++;
		console.log(addNum);
		$(this).prev().html(addNum);
		//价格计算
		var centerPrice = $('#centerPrice').html();
		var allCenterPrice = centerPrice * addNum;
		$('#allprice').html(allCenterPrice);
	});

	$('.add-l').on('tap', function() {
		var addNum = $(this).next().html();
		addNum = addNum - 1;
		console.log(addNum);
		$(this).next().html(addNum);
		if(addNum <= 1) {
			$(this).next().html(1);
			addNum = 1;
			mui.toast("年限已经最低啦")
		}
		//价格计算
		var centerPrice = $('#centerPrice').html();
		var allCenterPrice = centerPrice * addNum;
		$('#allprice').html(allCenterPrice);

	});

	//用户信息
	heardLook(function(data){
		$('.tou').attr({
			"src":data.user.userImg
		});
		$('.banner-bd-c p span:first-child').html(data.user.userName);
		$('.banner-bd-c p span:last-child').html(data.user.userTitle)
	});
	//年费剩余时间
	dayNumber({
		userId: 7
	}, function(data) {
		console.log(data);
		$('.vip span').html(data.data.remainDays);
		$('.jiage span').html(data.data.price);
		$('.riqi span').html(data.data.userStopTime);
		$('#centerPrice').html(data.data.price);
		$('#allprice').html(data.data.price)
	});
	
	$('.nianxian-btn').on('tap',function(){
		var allMoney = $('#allprice').html();
		var allYear = $('.add-c').html();
		var allDate = $('.riqi span').html();
		console.log(allMoney);
		console.log(allYear);
		console.log(allDate);
		
		var newCode = CD.geturl().code;
		console.log(newCode);
		var addTo = "http://www.leyouhui888.cn/weChatPay/getAccessToken?code=" + newCode + "&state=STATE";
		
		$.ajax({
					type: "get",
					url: addTo,
					data: '',
					dataType: 'json',
					success: function(data) {
						console.log(data);
						var getOpenId = data.data.openid;
						console.log(getOpenId);
						payTo({
							token: getCookieValue("token"),
							price: allMoney,
							openId: getOpenId,
							duration: allYear,
							gift_time_ratio: 1.0,
							superior_income_ratio: 0.0,
							expireDate:allDate
						}, function(data) {
							console.log(data);
							var oneTo = data.appId;
							var twoTo = data.timeStamp;
							var threeTo = data.nonceStr;
							var fourTo = data.package;
							var fiveTo = data.signType;
							var sixTo = data.paySign;
							console.log(fourTo);
							
							//调微信支付
							WeixinJSBridge.invoke('getBrandWCPayRequest', {
								"appId": oneTo,
								"timeStamp": twoTo,
								"nonceStr": threeTo,
								"package": fourTo,
								"signType": fiveTo,
								"paySign": sixTo,
								"jsApiList" : [ 'checkJsApi', 'startRecord', 'stopRecord','translateVoice','scanQRCode', 'openCard' ]
							}, function(res) {
								if(res.err_msg == "get_brand_wcpay_request:ok") {
									alert('支付成功');
									//支付成功后跳转的页面
									//Window.location.href='l-userlogin.html'
								} else if(res.err_msg == "get_brand_wcpay_request:cancel") {
									alert('支付取消');
								} else if(res.err_msg == "get_brand_wcpay_request:fail") {
									alert('支付失败');
									WeixinJSBridge.call('closeWindow');
								} //使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
							})
						});

					}
				});
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	});
})

//年费剩余时间
var dayNumber = function(params, callback) {
	$.ajax({
		type: "get",
		url: window.address + "/user/vipRemainDays",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: params,
		dataType: 'json',
		success: function(data) {
			console.log(data)
			callback && callback(data);
		}
	});
}

//个人信息
var heardLook = function(callback) {
	$.ajax({
		type: "post",
		cache: false,
		traditional: true,
		dataType: "json",
		url: window.address + "/user/findBayUserUuid",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: {
			"userUuid":CD.geturl().business
		},
		success: function(data) {
			console.log(data);
			callback && callback(data);
		}
	});
}

var payTo = function(params, callback) {
	$.ajax({
		type: "get",
		url: window.address + "/weChatPay/userPay",
		data: params,
		dataType: 'json',
		success: function(data) {
			console.log(data);
			callback && callback(data)
		}
	});
}